<template>
  <div id="login">
    <h2>用户登录</h2>
    <a-form
      label-align="left"
      auto-label-width
      style="max-width: 600px; margin: 0 auto"
      :model="form"
      @submit="handleSubmit"
    >
      <a-form-item
        field="userAccount"
        tooltip="Please enter username"
        label="账号"
        required
      >
        <a-input
          v-model="form.userAccount"
          placeholder="please enter your userAccount..."
        />
      </a-form-item>
      <a-form-item
        field="userPassword"
        label="密码"
        tooltip="Please enter password"
        required
      >
        <a-input
          v-model="form.userPassword"
          placeholder="please enter your userPassword..."
          type="password"
        />
      </a-form-item>
      <a-form-item>
        <!--        <a-button html-type="submit" type="primary" style="width: 120px"-->
        <!--          >注册-->
        <!--        </a-button>-->
        <a-button html-type="submit" type="primary" style="width: 120px"
          >登录
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import { UserControllerService, UserLoginRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

const form = reactive<UserLoginRequest>({
  userAccount: "",
  userPassword: "",
});

const router = useRouter();
const store = useStore();

const handleSubmit = async () => {
  const res = await UserControllerService.userLoginUsingPost(form);
  if (res.code === 0) {
    await store.dispatch("user/getLoginUser");
    await router.push({
      path: "/",
      replace: true,
    });
  } else {
    message.error(res.message);
  }
};
</script>
